home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Skunkware 5
/
Skunkware 5.iso
/
src
/
X11
/
stars
/
line.c
< prev
next >
Wrap
Text File
|
1995-05-09
|
3KB
|
121 lines
/*************************************************************************
* *
* Copyright (c) 1992, 1993 Ronald Joe Record *
* *
* All rights reserved. No part of this program or publication may be *
* reproduced, transmitted, transcribed, stored in a retrieval system, *
* or translated into any language or computer language, in any form or *
* by any means, electronic, mechanical, magnetic, optical, chemical, *
* biological, or otherwise, without the prior written permission of: *
* *
* Ronald Joe Record (408) 458-3718 *
* 212 Owen St., Santa Cruz, California 95062 USA *
* *
*************************************************************************/
#define ABSOLUTE_VALUE(a) (((a)<0) ? (0-(a)) : (a) )
typedef struct {
int x, y, u, v;
} xy_t;
/*
* mv_line() takes a pair of points, a distance, and an xy_t pointer
* and returns the xy_t pointer pointing to a point whose coordinates
* are those along the line from 'p0' to 'p1' distance 'dir'.
* The distance can be negative.
*/
xy_t *
mv_line(p0, p1, dir, newpt)
int dir;
xy_t p0, p1;
xy_t *newpt;
{
xy_t delta;
xy_t *move_vert_line();
xy_t *move_horz_line();
xy_t *move_steep_line();
xy_t *move_shallow_line();
delta.x = p0.x - p1.x;
delta.y = p0.y - p1.y;
if(0==delta.x)
move_vert_line(p0, p1, dir, newpt);
else if(0==delta.y)
move_horz_line(p0, p1, dir, newpt);
else if(ABSOLUTE_VALUE(delta.y) > ABSOLUTE_VALUE(delta.x))
move_steep_line(p0, p1, delta, dir, newpt);
else
move_shallow_line(p0, p1, delta, dir, newpt);
return(newpt);
}
xy_t *
move_steep_line(p0, p1, delta, dir, p2)
int dir;
xy_t p0, p1, delta;
xy_t *p2;
{
float slope;
p2[0].x = p0.x;
p2[0].y = p0.y;
if(p0.y > p1.y)
p2[0].y -= dir;
else
p2[0].y += dir;
slope = (float)delta.y / (float)delta.x;
p2[0].x = (((float)(p2[0].y - p1.y))/slope) + (float)p1.x;
return(p2);
}
xy_t *
move_shallow_line(p0, p1, delta, dir, p2)
int dir;
xy_t p0, p1, delta;
xy_t *p2;
{
float slope;
p2[0].x = p0.x;
p2[0].y = p0.y;
if(p0.x > p1.x)
p2[0].x -= dir;
else
p2[0].x += dir;
slope = (float)delta.y / (float)delta.x;
p2[0].y = (((float)(p2[0].x - p1.x)) * slope) + p1.y;
return(p2);
}
xy_t *
move_horz_line(p0, p1, dir, p2)
int dir;
xy_t p0, p1;
xy_t *p2;
{
p2[0].x = p0.x;
p2[0].y = p0.y;
if (p0.x > p1.x)
p2[0].x -= dir;
else
p2[0].x += dir;
return(p2);
}
xy_t *
move_vert_line(p0, p1, dir, p2)
int dir;
xy_t p0, p1;
xy_t *p2;
{
p2[0].x = p0.x;
p2[0].y = p0.y;
if (p0.y > p1.y)
p2[0].y -= dir;
else
p2[0].y += dir;
return(p2);
}